﻿//using Microsoft.Crm.Sdk.Messages;
//using Microsoft.Xrm.Sdk;
//using Microsoft.Xrm.Sdk.Query;
//using System;
//using System.Collections.Generic;
//using System.Linq;
//using System.Text;
//using System.Threading.Tasks;

//namespace isky.Plugin
//{
//    public class SJCBPlugin : Plugin
//    {
//        private readonly string _unsecureString;
//        private readonly string _secureString;
//        public SJCBPlugin(string unsecureString, string secureString)
//   : base(typeof(SJCBPlugin))
//        {
//            this._secureString = secureString;
//            this._unsecureString = unsecureString;
//            base.RegisteredEvents.Add(new Tuple<int, string, string, Action<LocalPluginContext>>(40, "Create", "new_chengben", new Action<LocalPluginContext>(ChengbenPreCreate)));
//            base.RegisteredEvents.Add(new Tuple<int, string, string, Action<LocalPluginContext>>(40, "Update", "new_chengben", new Action<LocalPluginContext>(ChengbenPreUpdate)));
//            base.RegisteredEvents.Add(new Tuple<int, string, string, Action<LocalPluginContext>>(20, "RetrieveMultiple", "new_chengben", new Action<LocalPluginContext>(ChengbenPostFilter)));

//        }

//        private void ChengbenPostFilter(LocalPluginContext localContext)
//        {
//            if (localContext == null)
//            {
//                throw new ArgumentNullException("localContext");
//            }
//            try
//            {
//                IPluginExecutionContext context = localContext.PluginExecutionContext;
//                IOrganizationService service = localContext.OrganizationService;
//                var tracingService = localContext.TracingService;
                
//                if (context.InputParameters.ContainsKey("Query"))
//                {
//                    foreach (var item in context.InputParameters.Values)
//                    {
                       
//                    }
                   
//                    //(context.InputParameters.Values).Items[0]).ColumnSet.Columns).Items[16]

//                    if (context.InputParameters["Query"] is FetchExpression)
//                    {
//                        FetchExpression fetch = (FetchExpression)context.InputParameters["Query"];
//                        var conversionRequest = new FetchXmlToQueryExpressionRequest
//                        {
//                            FetchXml = fetch.Query
//                        };
//                        var conversionResponse =
//                            (FetchXmlToQueryExpressionResponse)service.Execute(conversionRequest);
//                        // Use the newly converted query expression to make a retrieve multiple
//                        // request to Microsoft Dynamics CRM.
//                        QueryExpression queryExpression = conversionResponse.Query;

//                        if (queryExpression.EntityName == "new_chengben" && context.Depth == 1)
//                        {
//                            //逻辑
//                        }
//                        // Convert the query expression to FetchXML.
//                        var converRequest = new QueryExpressionToFetchXmlRequest
//                        {
//                            Query = queryExpression
//                        };
//                        var converResponse =
//                            (QueryExpressionToFetchXmlResponse)service.Execute(converRequest);

//                        // Use the converted query to make a retrieve multiple request to Microsoft Dynamics CRM.
//                        String fetchXml = converResponse.FetchXml;
//                        fetch.Query = fetchXml;
//                    }
//                }




//                if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
//                {
//                    Entity inputEntity = context.InputParameters["Target"] as Entity;
//                    EntityReference ygcbId = inputEntity.GetAttributeValue<EntityReference>("new_chengbenId");
//                    if (inputEntity != null)
//                    {
//                        //预计利润
//                        string yjlr = inputEntity.GetAttributeValue<string>("new_yjlr");
//                        if (yjlr == null) return;
//                        //预计重量
//                        string zhongliang = inputEntity.GetAttributeValue<string>("new_zhongliang");
//                        inputEntity.Attributes["new_profites"] = Convert.ToInt32(yjlr) * Convert.ToInt32(zhongliang);
//                    }
//                }

//            }
//            catch (Exception ex)
//            {
//                throw new InvalidPluginExecutionException(ex.Message);
//                throw new NotImplementedException();
//            }
//        }



//        private Entity GetSystemuser(IOrganizationService service, Guid UserID)
//        {
//            return service.Retrieve("new_chengben", UserID, new ColumnSet("new_chengbenid"));
//        }


//        private void ChengbenPreCreate(LocalPluginContext localContext)
//        {
//            try
//            {
//                if (localContext == null)
//                    throw new InvalidPluginExecutionException("localContext");
//                IPluginExecutionContext context = localContext.PluginExecutionContext;
//                IOrganizationService service = localContext.OrganizationService;
//                var tracingService = localContext.TracingService;
//                if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
//                {
//                    Entity inputEntity = context.InputParameters["Target"] as Entity;
//                    EntityReference ygcbId = inputEntity.GetAttributeValue<EntityReference>("new_chengbenId");
//                    if (inputEntity != null)
//                    {
//                        //预计利润
//                        string yjlr = inputEntity.GetAttributeValue<string>("new_yjlr");
//                        if (yjlr == null) return;
//                        //预计重量
//                        string zhongliang = inputEntity.GetAttributeValue<string>("new_zhongliang");
//                        inputEntity.Attributes["new_profites"] = Convert.ToInt32(yjlr) * Convert.ToInt32(zhongliang);
//                    }
//                }
//            }
//            catch (Exception ex)
//            {
//                throw new InvalidPluginExecutionException(ex.Message);
//                throw new NotImplementedException();
//            }
//        }
//        private void ChengbenPreUpdate(LocalPluginContext localContext)
//        {
//            try
//            {
//                if (localContext == null)
//                    throw new InvalidPluginExecutionException("localContext");
//                IPluginExecutionContext context = localContext.PluginExecutionContext;
//                IOrganizationService service = localContext.OrganizationService;
//                var tracingService = localContext.TracingService;
//                if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
//                {
//                    Entity inputEntity = context.InputParameters["Target"] as Entity;
//                    EntityReference ygcbId = inputEntity.GetAttributeValue<EntityReference>("new_chengbenId");
//                    if (inputEntity != null)
//                    {
//                        //预计利润
//                        string yjlr = inputEntity.GetAttributeValue<string>("new_yjlr");
//                        if (yjlr == null) return;
//                        //预计重量
//                        string zhongliang = inputEntity.GetAttributeValue<string>("new_zhongliang");
//                        inputEntity.Attributes["new_profites"] = Convert.ToInt32(yjlr) * Convert.ToInt32(zhongliang);
//                    }
//                }
//            }
//            catch (Exception ex)
//            {
//                throw new InvalidPluginExecutionException(ex.Message);
//                throw new NotImplementedException();
//            }
//        }

//    }
//}
